use std::collections::HashMap;
use crate::storage::Storage;

/// The `KvStore` stores string key/value pairs.
///
/// Key/value pairs are stored in a `HashMap` in memory and not persisted to disk.
///
/// Example:
///
/// ```rust
/// # use kvs::KvStore;
/// let mut store = KvStore::new();
/// store.set("key".to_owned(), "value".to_owned());
/// let val = store.get("key".to_owned());
/// assert_eq!(val, Some("value".to_owned()));
/// ```
pub struct HashStorage {
    map: HashMap<String, String>
}

impl Storage for HashStorage {
    fn get(&mut self, key: String) -> Option<String> {
        self.map.get(&key).cloned()
    }

    fn set(&mut self, key: String, value: String) {
        self.map.insert(key, value);
    }

    fn remove(&mut self, key: String) {
        self.map.remove(&key);
    }
}

impl HashStorage {
    pub fn new() -> Self {
        HashStorage {
            map: HashMap::new()
        }
    }
    #[warn(dead_code)]
    pub fn with_capacity(capacity: usize) -> Self {
        HashStorage {
            map: HashMap::with_capacity(capacity)
        }
    }
}